package com.gnerv.battle.boot.project.guard.business.impl;

import com.gnerv.battle.boot.common.base.model.RestResult;
import com.gnerv.battle.boot.common.tools.UUIDTools;
import com.gnerv.battle.boot.project.guard.business.MonitorHostBusiness;
import com.gnerv.battle.boot.project.guard.model.bo.MonitorHostBO;
import com.gnerv.battle.boot.project.guard.model.dto.MonitorHostCreateDTO;
import com.gnerv.battle.boot.project.guard.model.dto.MonitorHostDTO;
import com.gnerv.battle.boot.project.guard.model.dto.MonitorHostUpdateDTO;
import com.gnerv.battle.boot.project.guard.service.MonitorHostService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Gnerv LiGen
 */
@Slf4j
@Service
@AllArgsConstructor
public class MonitorHostBusinessImpl implements MonitorHostBusiness {

    private final MonitorHostService monitorHostService;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> create(MonitorHostCreateDTO monitorHostCreateDTO) {
        MonitorHostBO monitorHostBO = monitorHostCreateDTO.toMonitorHostBO();
        monitorHostBO.setUkId(UUIDTools.lower());
        monitorHostService.create(monitorHostBO);
        return RestResult.ok();
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> deleteByUkId(String ukId) {
        monitorHostService.deleteByUkId(ukId);
        return RestResult.ok();
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public RestResult<Void> updateByUkId(MonitorHostUpdateDTO monitorHostUpdateDTO) {
        monitorHostService.updateByUkId(monitorHostUpdateDTO.toMonitorHostBO());
        return RestResult.ok();
    }

    @Override
    @Transactional(readOnly = true)
    public RestResult<MonitorHostDTO> selectByUkId(String ukId) {
        return RestResult.ok(monitorHostService.selectByUkId(ukId));
    }

    @Override
    @Transactional(readOnly = true)
    public RestResult<List<MonitorHostDTO>> listByName(String name, Integer page, Integer size) {
        return RestResult.ok(monitorHostService.listByName(name, page, size));
    }

}
